/*
 * @(#)Conclusion.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.lang;

/**
 * Clase que describe la conclusi�n de una regla difusa
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class Conclusion {
	
	//----------------------------------------------------------------------------//
	//                             MIEMBROS PRIVADOS                              //
	//----------------------------------------------------------------------------//
	
	/**
	 * Variable de salida a la que se refiere la conclusi�n
	 */
	private Variable var;
	
	/**
	 * Funci�n de pertenencia de la variable de salida a la que se refiere
	 * la conclusi�n
	 */
	private LinguisticLabel mf;
	
	/**
	 * Base de reglas a la que pertenece la conclusi�n (necesaria para
	 * obtener los operadores)
	 */
	private KnowledgeBase mod;
	
	/**
	 * Resultado de una ejecuci�n de la base de reglas
	 */
	private ImpliedMemFunc imf;
	
	//----------------------------------------------------------------------------//
	//                               CONSTRUCTOR                                  //
	//----------------------------------------------------------------------------//
	
	/**
	 * Constructor
	 */
	public Conclusion(Variable var, LinguisticLabel mf, KnowledgeBase mod) {
		this.var = var;
		this.var.link();
		this.mf = mf;
		this.mf.link();
		this.mod = mod;
	}
	
	//----------------------------------------------------------------------------//
	//                            M�TODOS P�BLICOS                                //
	//----------------------------------------------------------------------------//
	
	/**
	 * Ejecuta la conclusi�n con un cierto grado de activaci�n
	 */
	public void compute(double degree) {
		this.imf = new ImpliedMemFunc(mf,mod,degree);
		this.var.addConclusion(this.imf);
	}
		
	/**
	 * Obtiene la derivada del grado de activaci�n
	 */
	public double getDegreeDeriv() {
		return this.imf.getDegreeDeriv();
	}
		
	/**
	 * Obtiene un duplicado del objeto
	 */
	public Object clone(KnowledgeBase rbclone) {
		return new Conclusion(this.var, this.mf, rbclone);
	}
	
	/**
	 * Elimina los enlaces del objeto
	 */
	public void dispose() {
		this.var.unlink();
		this.mf.unlink();
	}
	
	/**
	 * Obtiene la referencia a la variable de la conclusi�n
	 */
	public Variable getVariable() {
		return this.var;
	}
	
	/**
	 * Obtiene la referencia a la MF de la conclusi�n
	 */
	public LinguisticLabel getMembershipFunction() {
		return this.mf;
	}
	
	/**
	 * Asigna la variable de la conclusi�n
	 */
	public void setVariable(Variable var) {
		this.var = var;
	}
	
	/**
	 * Asigna la funci�n de pertenencia de la conclusi�n
	 */
	public void setMembershipFunction(LinguisticLabel mf) {
		this.mf = mf;
	}
	
	/**
	 * Intercambia dos funciones de pertenencia
	 */
	public void exchange(LinguisticLabel oldmf, LinguisticLabel newmf) {
		if(this.mf == oldmf) {
			this.mf.unlink();
			this.mf = newmf;
			this.mf.link();
		}
	}
		
	/**
	 * Intercambia dos variables
	 */
	public void exchange(Variable oldvar, Variable newvar) {
		if(this.var == oldvar) {
			this.var.unlink();
			this.var = newvar;
			this.var.link();
		}
	}
		
	/**
	 * Descripci�n XFL3 del objeto
	 */
	public String toXfl() {
		return this.var.getName() + " = " + this.mf;
	}
	
}
